﻿using System;
using System.Net;
using System.Text;
using System.Collections.Generic;
using System.Web;

namespace Xushre.WeiBoSDK {
    /// <summary>
    /// 工具类
    /// </summary>
    public class Tools {

        /// <summary>
        /// 模拟 HTTP 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="method"></param>
        /// <param name="data"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        /// <exception cref="WeiboException"></exception>
        public static byte[] Request(string url, string method = "GET", string data = null, Encoding encoding = null) {
            if (encoding == null) {
                encoding = Encoding.UTF8;
            }

            // 用于将数据发送到资源的 HTTP 方法
            method = method.ToUpper();

            byte[] response = new byte[] { };

            try {
                WebClient client = new WebClient();
                client.Encoding = encoding;

                if (method == "GET") {
                    url += (url.IndexOf("?") > 0 ? "&" : "?") + data;

                    response = client.DownloadData(url); ;
                } else {
                    if (method == "POST") {
                        client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                    }
                    // System.IO.File.WriteAllText("D:/log.txt", data);
                    response = client.UploadData(url, method, encoding.GetBytes(data));
                }

            } catch (WebException ex) {
                System.IO.Stream resp = ex.Response.GetResponseStream();
                response = new byte[ex.Response.ContentLength];
                resp.Read(response, 0, response.Length);
            } catch (Exception e) {
                throw new WeiboException(e.Message, e.InnerException);
            }

            return response;
        }

        /// <summary>
        /// 模拟 HTTP 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="method"></param>
        /// <param name="parameters"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        /// <exception cref="WeiboException"></exception>
        public static byte[] Request(string url, string method = "GET", Dictionary<string, object> parameters = null, Encoding encoding = null) {
            if (encoding == null) {
                encoding = Encoding.UTF8;
            }

            // 用于将数据发送到资源的 HTTP 方法
            method = method.ToUpper();

            string data = null;

            if (parameters.Count > 0) {
                string[] list = new string[parameters.Count];
                try {
                    int i = 0;
                    foreach (var key in parameters.Keys) {
                        list[i] = String.Format("{0}={1}", key.ToString(), HttpUtility.UrlEncode(parameters[key.ToString()].ToString()));
                        i++;
                    }
                } catch (Exception e) {
                    throw new WeiboException(e.Message, e.InnerException);
                }

                data = String.Join("&", list);
            }

            byte[] response = new byte[] { };

            try {
                response = Request(url, method, data: data, encoding: encoding);
            } catch (Exception e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 模拟 HTTP POST
        /// </summary>
        /// <param name="url"></param>
        /// <param name="parameters"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        /// <exception cref="WeiboException"></exception>
        public static byte[] Post(string url, Dictionary<string, object> parameters = null, Encoding encoding = null) {
            byte[] response = new byte[] { };

            try {
                response = Request(url, "POST", parameters: parameters, encoding: encoding);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 模拟 HTTP POST
        /// </summary>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        /// <exception cref="WeiboException"></exception>
        public static byte[] Post(string url, string data = null, Encoding encoding = null) {
            byte[] response = new byte[] { };

            try {
                response = Request(url, "POST", data: data, encoding: encoding);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 模拟 HTTP GET
        /// </summary>
        /// <param name="url"></param>
        /// <param name="parameters"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        /// <exception cref="WeiboException"></exception>
        public static byte[] Get(string url, Dictionary<string, object> parameters = null, Encoding encoding = null) {
            byte[] response = new byte[] { };

            try {
                response = Request(url, "GET", parameters: parameters, encoding: encoding);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 模拟 HTTP GET
        /// </summary>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        /// <exception cref="WeiboException"></exception>
        public static byte[] Get(string url, string data = null, Encoding encoding = null) {
            byte[] response = new byte[] { };

            try {
                response = Request(url, "GET", data: data, encoding: encoding);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 判断字符串是否含有非单字节字符
        /// </summary>
        /// <param name="s">要判断的字符串</param>
        /// <returns></returns>
        public static bool isSingle(string s) {
            bool _isSingle = false;
            for (int i = 0; i < s.Length - 1; i++) {
                if (System.Text.Encoding.Default.GetByteCount(s.Substring(i, 1)) > 1) {
                    _isSingle = true;
                    break;
                }
            }

            return _isSingle;
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="url">接收文件 URL</param>
        /// <param name="file">上传的文件</param>
        /// <param name="data">表单内容</param>
        /// <param name="encoding">字符编码。默认是 UTF8</param>
        /// <returns></returns>
        /// <exception cref="WeiboException"></exception>
        public static byte[] UploadFile(string url, Dictionary<string, string> file, Dictionary<string, object> data = null, Encoding encoding = null) {
            if (encoding == null) {
                encoding = Encoding.UTF8;
            }

            System.Collections.ArrayList bytesArray = new System.Collections.ArrayList();

            try {
                // 普通表单
                if (data != null && data.Count > 0) {
                    foreach (string name in data.Keys) {
                        bytesArray.Add(CreateFieldData(encoding, name, HttpUtility.UrlEncode(data[name].ToString())));
                    }
                }
            } catch (Exception e) {
                throw new WeiboException(e.Message, e.InnerException);
            }

            try {
                // 文件表单
                foreach (string name in file.Keys) {
                    System.IO.FileInfo fi = new System.IO.FileInfo(file[name]);

                    if (fi.Exists == false) {
                        throw new WeiboException("File not found: " + file[name]);
                    }

                    System.IO.FileStream fs = new System.IO.FileStream(file[name], System.IO.FileMode.Open, System.IO.FileAccess.Read);
                    byte[] fileBytes = new byte[fs.Length];
                    fs.Read(fileBytes, 0, (int) fs.Length);
                    fs.Close();

                    string contentType = GetContentType(fi.Extension);

                    bytesArray.Add(CreateFieldData(encoding, name, HttpUtility.UrlEncode(fi.Name), contentType, fileBytes));
                }
            } catch (Exception e) {
                throw new WeiboException(e.Message, e.InnerException);
            }

            byte[] requestData = JoinBytes(encoding, bytesArray);

            byte[] response = new byte[] { };

            try {
                WebClient client = new WebClient();
                client.Encoding = encoding;
                client.Headers.Add("Content-Type", ContentType);
                response = client.UploadData(url, requestData);
            } catch (WebException ex) {
                System.IO.Stream resp = ex.Response.GetResponseStream();
                response = new byte[ex.Response.ContentLength];
                resp.Read(response, 0, response.Length);
            } catch (Exception e) {
                throw new WeiboException(e.Message, e.InnerException);
            }

            return response;
        }

        /// <summary>
        /// 获取普通表单区域二进制数组
        /// </summary>
        /// <param name="encoding">字符编码</param>
        /// <param name="name">表单名</param>
        /// <param name="value">表单值</param>
        /// <returns></returns>
        /// <remarks>
        /// -----------------------------7d52ee27210a3c\r\nContent-Disposition: form-data; name=\"表单名\"\r\n\r\n表单值\r\n
        /// </remarks>
        private static byte[] CreateFieldData(Encoding encoding, string name, string value) {
            string textTemplate = Boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}\r\n";
            string text = String.Format(textTemplate, name, value.ToString());
            byte[] bytes = encoding.GetBytes(text);
            return bytes;
        }

        /// <summary>
        /// 获取文件上传表单区域二进制数组
        /// </summary>
        /// <param name="encoding">字符编码</param>
        /// <param name="name">表单名</param>
        /// <param name="filename">文件名</param>
        /// <param name="contentType">文件类型</param>
        /// <param name="fileBytes">文件流</param>
        /// <returns>二进制数组</returns>
        private static byte[] CreateFieldData(Encoding encoding, string name, string filename, string contentType, byte[] fileBytes) {
            string end = "\r\n";
            string textTemplate = Boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";

            // 头数据
            string data = String.Format(textTemplate, name, filename, contentType);
            byte[] bytes = encoding.GetBytes(data);

            // 尾数据
            byte[] endBytes = encoding.GetBytes(end);

            // 合成后的数组
            byte[] fieldData = new byte[bytes.Length + fileBytes.Length + endBytes.Length];

            bytes.CopyTo(fieldData, 0); // 头数据
            fileBytes.CopyTo(fieldData, bytes.Length); // 文件的二进制数据
            endBytes.CopyTo(fieldData, bytes.Length + fileBytes.Length); // \r\n

            return fieldData;
        }


        /// <summary>
        /// 拼接所有的二进制数组为一个数组
        /// </summary>
        /// <param name="encoding">字符编码</param>
        /// <param name="byteArrays">数组</param>
        /// <returns>加上结束边界</returns>
        private static byte[] JoinBytes(Encoding encoding, System.Collections.ArrayList byteArrays) {
            int length = 0;
            int readLength = 0;

            // 加上结束边界
            string endBoundary = Boundary + "--\r\n"; //结束边界
            byte[] endBoundaryBytes = encoding.GetBytes(endBoundary);
            byteArrays.Add(endBoundaryBytes);

            foreach (byte[] b in byteArrays) {
                length += b.Length;
            }
            byte[] bytes = new byte[length];

            // 遍历复制
            foreach (byte[] b in byteArrays) {
                b.CopyTo(bytes, readLength);
                readLength += b.Length;
            }

            return bytes;
        }

        /// <summary>
        /// 根据文件后辍名获取文件 MIME 类型
        /// </summary>
        /// <param name="extension">文件后辍名</param>
        /// <returns>MIME 类型</returns>
        public static string GetContentType(string extension) {
            string type = string.Empty;

            System.Collections.Generic.Dictionary<string, string> data = new Dictionary<string, string>();
            data.Add(".3gp", "video/3gpp");
            data.Add(".aab", "application/x-authoware-bin");
            data.Add(".aam", "application/x-authoware-map");
            data.Add(".aas", "application/x-authoware-seg");
            data.Add(".ai", "application/postscript");
            data.Add(".aif", "audio/x-aiff");
            data.Add(".aifc", "audio/x-aiff");
            data.Add(".aiff", "audio/x-aiff");
            data.Add(".als", "audio/X-Alpha5");
            data.Add(".amc", "application/x-mpeg");
            data.Add(".ani", "application/octet-stream");
            data.Add(".asc", "text/plain");
            data.Add(".asd", "application/astound");
            data.Add(".asf", "video/x-ms-asf");
            data.Add(".asn", "application/astound");
            data.Add(".asp", "application/x-asap");
            data.Add(".asx", "video/x-ms-asf");
            data.Add(".au", "audio/basic");
            data.Add(".avb", "application/octet-stream");
            data.Add(".avi", "video/x-msvideo");
            data.Add(".awb", "audio/amr-wb");
            data.Add(".bcpio", "application/x-bcpio");
            data.Add(".bin", "application/octet-stream");
            data.Add(".bld", "application/bld");
            data.Add(".bld2", "application/bld2");
            data.Add(".bmp", "application/x-MS-bmp");
            data.Add(".bpk", "application/octet-stream");
            data.Add(".bz2", "application/x-bzip2");
            data.Add(".cal", "image/x-cals");
            data.Add(".ccn", "application/x-cnc");
            data.Add(".cco", "application/x-cocoa");
            data.Add(".cdf", "application/x-netcdf");
            data.Add(".cgi", "magnus-internal/cgi");
            data.Add(".chat", "application/x-chat");
            data.Add(".class", "application/octet-stream");
            data.Add(".clp", "application/x-msclip");
            data.Add(".cmx", "application/x-cmx");
            data.Add(".co", "application/x-cult3d-object");
            data.Add(".cod", "image/cis-cod");
            data.Add(".cpio", "application/x-cpio");
            data.Add(".cpt", "application/mac-compactpro");
            data.Add(".crd", "application/x-mscardfile");
            data.Add(".csh", "application/x-csh");
            data.Add(".csm", "chemical/x-csml");
            data.Add(".csml", "chemical/x-csml");
            data.Add(".css", "text/css");
            data.Add(".cur", "application/octet-stream");
            data.Add(".dcm", "x-lml/x-evm");
            data.Add(".dcr", "application/x-director");
            data.Add(".dcx", "image/x-dcx");
            data.Add(".dhtml", "text/html");
            data.Add(".dir", "application/x-director");
            data.Add(".dll", "application/octet-stream");
            data.Add(".dmg", "application/octet-stream");
            data.Add(".dms", "application/octet-stream");
            data.Add(".doc", "application/msword");
            data.Add(".dot", "application/x-dot");
            data.Add(".dvi", "application/x-dvi");
            data.Add(".dwf", "drawing/x-dwf");
            data.Add(".dwg", "application/x-autocad");
            data.Add(".dxf", "application/x-autocad");
            data.Add(".dxr", "application/x-director");
            data.Add(".ebk", "application/x-expandedbook");
            data.Add(".emb", "chemical/x-embl-dl-nucleotide");
            data.Add(".embl", "chemical/x-embl-dl-nucleotide");
            data.Add(".eps", "application/postscript");
            data.Add(".epub", "application/epub+zip");
            data.Add(".eri", "image/x-eri");
            data.Add(".es", "audio/echospeech");
            data.Add(".esl", "audio/echospeech");
            data.Add(".etc", "application/x-earthtime");
            data.Add(".etx", "text/x-setext");
            data.Add(".evm", "x-lml/x-evm");
            data.Add(".evy", "application/x-envoy");
            data.Add(".exe", "application/octet-stream");
            data.Add(".fh4", "image/x-freehand");
            data.Add(".fh5", "image/x-freehand");
            data.Add(".fhc", "image/x-freehand");
            data.Add(".fif", "image/fif");
            data.Add(".fm", "application/x-maker");
            data.Add(".fpx", "image/x-fpx");
            data.Add(".fvi", "video/isivideo");
            data.Add(".gau", "chemical/x-gaussian-input");
            data.Add(".gca", "application/x-gca-compressed");
            data.Add(".gdb", "x-lml/x-gdb");
            data.Add(".gif", "image/gif");
            data.Add(".gps", "application/x-gps");
            data.Add(".gtar", "application/x-gtar");
            data.Add(".gz", "application/x-gzip");
            data.Add(".hdf", "application/x-hdf");
            data.Add(".hdm", "text/x-hdml");
            data.Add(".hdml", "text/x-hdml");
            data.Add(".hlp", "application/winhlp");
            data.Add(".hqx", "application/mac-binhex40");
            data.Add(".htm", "text/html");
            data.Add(".html", "text/html");
            data.Add(".hts", "text/html");
            data.Add(".ice", "x-conference/x-cooltalk");
            data.Add(".ico", "application/octet-stream");
            data.Add(".ief", "image/ief");
            data.Add(".ifm", "image/gif");
            data.Add(".ifs", "image/ifs");
            data.Add(".imy", "audio/melody");
            data.Add(".ins", "application/x-NET-Install");
            data.Add(".ips", "application/x-ipscript");
            data.Add(".ipx", "application/x-ipix");
            data.Add(".it", "audio/x-mod");
            data.Add(".itz", "audio/x-mod");
            data.Add(".ivr", "i-world/i-vrml");
            data.Add(".j2k", "image/j2k");
            data.Add(".jad", "text/vnd.sun.j2me.app-descriptor");
            data.Add(".jam", "application/x-jam");
            data.Add(".jar", "application/java-archive");
            data.Add(".jnlp", "application/x-java-jnlp-file");
            data.Add(".jpe", "image/jpeg");
            data.Add(".jpeg", "image/jpeg");
            data.Add(".jpg", "image/jpeg");
            data.Add(".jpz", "image/jpeg");
            data.Add(".js", "application/x-javascript");
            data.Add(".jwc", "application/jwc");
            data.Add(".kjx", "application/x-kjx");
            data.Add(".lak", "x-lml/x-lak");
            data.Add(".latex", "application/x-latex");
            data.Add(".lcc", "application/fastman");
            data.Add(".lcl", "application/x-digitalloca");
            data.Add(".lcr", "application/x-digitalloca");
            data.Add(".lgh", "application/lgh");
            data.Add(".lha", "application/octet-stream");
            data.Add(".lml", "x-lml/x-lml");
            data.Add(".lmlpack", "x-lml/x-lmlpack");
            data.Add(".lsf", "video/x-ms-asf");
            data.Add(".lsx", "video/x-ms-asf");
            data.Add(".lzh", "application/x-lzh");
            data.Add(".m13", "application/x-msmediaview");
            data.Add(".m14", "application/x-msmediaview");
            data.Add(".m15", "audio/x-mod");
            data.Add(".m3u", "audio/x-mpegurl");
            data.Add(".m3url", "audio/x-mpegurl");
            data.Add(".ma1", "audio/ma1");
            data.Add(".ma2", "audio/ma2");
            data.Add(".ma3", "audio/ma3");
            data.Add(".ma5", "audio/ma5");
            data.Add(".man", "application/x-troff-man");
            data.Add(".map", "magnus-internal/imagemap");
            data.Add(".mbd", "application/mbedlet");
            data.Add(".mct", "application/x-mascot");
            data.Add(".mdb", "application/msaccess");
            data.Add(".mdz", "audio/x-mod");
            data.Add(".me", "application/x-troff-me");
            data.Add(".mel", "text/x-vmel");
            data.Add(".mi", "application/x-mif");
            data.Add(".mid", "audio/midi");
            data.Add(".midi", "audio/midi");
            data.Add(".mif", "application/x-mif");
            data.Add(".mil", "image/x-cals");
            data.Add(".mio", "audio/x-mio");
            data.Add(".mmf", "application/x-skt-lbs");
            data.Add(".mng", "video/x-mng");
            data.Add(".mny", "application/x-msmoney");
            data.Add(".moc", "application/x-mocha");
            data.Add(".mocha", "application/x-mocha");
            data.Add(".mod", "audio/x-mod");
            data.Add(".mof", "application/x-yumekara");
            data.Add(".mol", "chemical/x-mdl-molfile");
            data.Add(".mop", "chemical/x-mopac-input");
            data.Add(".mov", "video/quicktime");
            data.Add(".movie", "video/x-sgi-movie");
            data.Add(".mp2", "audio/x-mpeg");
            data.Add(".mp3", "audio/x-mpeg");
            data.Add(".mp4", "video/mp4");
            data.Add(".mpc", "application/vnd.mpohun.certificate");
            data.Add(".mpe", "video/mpeg");
            data.Add(".mpeg", "video/mpeg");
            data.Add(".mpg", "video/mpeg");
            data.Add(".mpg4", "video/mp4");
            data.Add(".mpga", "audio/mpeg");
            data.Add(".mpn", "application/vnd.mophun.application");
            data.Add(".mpp", "application/vnd.ms-project");
            data.Add(".mps", "application/x-mapserver");
            data.Add(".mrl", "text/x-mrml");
            data.Add(".mrm", "application/x-mrm");
            data.Add(".ms", "application/x-troff-ms");
            data.Add(".mts", "application/metastream");
            data.Add(".mtx", "application/metastream");
            data.Add(".mtz", "application/metastream");
            data.Add(".mzv", "application/metastream");
            data.Add(".nar", "application/zip");
            data.Add(".nbmp", "image/nbmp");
            data.Add(".nc", "application/x-netcdf");
            data.Add(".ndb", "x-lml/x-ndb");
            data.Add(".ndwn", "application/ndwn");
            data.Add(".nif", "application/x-nif");
            data.Add(".nmz", "application/x-scream");
            data.Add(".nokia-op-logo", "image/vnd.nok-oplogo-color");
            data.Add(".npx", "application/x-netfpx");
            data.Add(".nsnd", "audio/nsnd");
            data.Add(".nva", "application/x-neva1");
            data.Add(".oda", "application/oda");
            data.Add(".oom", "application/x-AtlasMate-Plugin");
            data.Add(".pac", "audio/x-pac");
            data.Add(".pae", "audio/x-epac");
            data.Add(".pan", "application/x-pan");
            data.Add(".pbm", "image/x-portable-bitmap");
            data.Add(".pcx", "image/x-pcx");
            data.Add(".pda", "image/x-pda");
            data.Add(".pdb", "chemical/x-pdb");
            data.Add(".pdf", "application/pdf");
            data.Add(".pfr", "application/font-tdpfr");
            data.Add(".pgm", "image/x-portable-graymap");
            data.Add(".pict", "image/x-pict");
            data.Add(".pm", "application/x-perl");
            data.Add(".pmd", "application/x-pmd");
            data.Add(".png", "image/png");
            data.Add(".pnm", "image/x-portable-anymap");
            data.Add(".pnz", "image/png");
            data.Add(".pot", "application/vnd.ms-powerpoint");
            data.Add(".ppm", "image/x-portable-pixmap");
            data.Add(".pps", "application/vnd.ms-powerpoint");
            data.Add(".ppt", "application/vnd.ms-powerpoint");
            data.Add(".pqf", "application/x-cprplayer");
            data.Add(".pqi", "application/cprplayer");
            data.Add(".prc", "application/x-prc");
            data.Add(".proxy", "application/x-ns-proxy-autoconfig");
            data.Add(".ps", "application/postscript");
            data.Add(".ptlk", "application/listenup");
            data.Add(".pub", "application/x-mspublisher");
            data.Add(".pvx", "video/x-pv-pvx");
            data.Add(".qcp", "audio/vnd.qcelp");
            data.Add(".qt", "video/quicktime");
            data.Add(".qti", "image/x-quicktime");
            data.Add(".qtif", "image/x-quicktime");
            data.Add(".r3t", "text/vnd.rn-realtext3d");
            data.Add(".ra", "audio/x-pn-realaudio");
            data.Add(".ram", "audio/x-pn-realaudio");
            data.Add(".rar", "application/octet-stream");
            data.Add(".ras", "image/x-cmu-raster");
            data.Add(".rdf", "application/rdf+xml");
            data.Add(".rf", "image/vnd.rn-realflash");
            data.Add(".rgb", "image/x-rgb");
            data.Add(".rlf", "application/x-richlink");
            data.Add(".rm", "audio/x-pn-realaudio");
            data.Add(".rmf", "audio/x-rmf");
            data.Add(".rmm", "audio/x-pn-realaudio");
            data.Add(".rmvb", "audio/x-pn-realaudio");
            data.Add(".rnx", "application/vnd.rn-realplayer");
            data.Add(".roff", "application/x-troff");
            data.Add(".rp", "image/vnd.rn-realpix");
            data.Add(".rpm", "audio/x-pn-realaudio-plugin");
            data.Add(".rt", "text/vnd.rn-realtext");
            data.Add(".rte", "x-lml/x-gps");
            data.Add(".rtf", "application/rtf");
            data.Add(".rtg", "application/metastream");
            data.Add(".rtx", "text/richtext");
            data.Add(".rv", "video/vnd.rn-realvideo");
            data.Add(".rwc", "application/x-rogerwilco");
            data.Add(".s3m", "audio/x-mod");
            data.Add(".s3z", "audio/x-mod");
            data.Add(".sca", "application/x-supercard");
            data.Add(".scd", "application/x-msschedule");
            data.Add(".sdf", "application/e-score");
            data.Add(".sea", "application/x-stuffit");
            data.Add(".sgm", "text/x-sgml");
            data.Add(".sgml", "text/x-sgml");
            data.Add(".sh", "application/x-sh");
            data.Add(".shar", "application/x-shar");
            data.Add(".shtml", "magnus-internal/parsed-html");
            data.Add(".shw", "application/presentations");
            data.Add(".si6", "image/si6");
            data.Add(".si7", "image/vnd.stiwap.sis");
            data.Add(".si9", "image/vnd.lgtwap.sis");
            data.Add(".sis", "application/vnd.symbian.install");
            data.Add(".sit", "application/x-stuffit");
            data.Add(".skd", "application/x-Koan");
            data.Add(".skm", "application/x-Koan");
            data.Add(".skp", "application/x-Koan");
            data.Add(".skt", "application/x-Koan");
            data.Add(".slc", "application/x-salsa");
            data.Add(".smd", "audio/x-smd");
            data.Add(".smi", "application/smil");
            data.Add(".smil", "application/smil");
            data.Add(".smp", "application/studiom");
            data.Add(".smz", "audio/x-smd");
            data.Add(".snd", "audio/basic");
            data.Add(".spc", "text/x-speech");
            data.Add(".spl", "application/futuresplash");
            data.Add(".spr", "application/x-sprite");
            data.Add(".sprite", "application/x-sprite");
            data.Add(".sdp", "application/sdp");
            data.Add(".spt", "application/x-spt");
            data.Add(".src", "application/x-wais-source");
            data.Add(".stk", "application/hyperstudio");
            data.Add(".stm", "audio/x-mod");
            data.Add(".sv4cpio", "application/x-sv4cpio");
            data.Add(".sv4crc", "application/x-sv4crc");
            data.Add(".svf", "image/vnd");
            data.Add(".svg", "image/svg-xml");
            data.Add(".svh", "image/svh");
            data.Add(".svr", "x-world/x-svr");
            data.Add(".swf", "application/x-shockwave-flash");
            data.Add(".swfl", "application/x-shockwave-flash");
            data.Add(".t", "application/x-troff");
            data.Add(".tad", "application/octet-stream");
            data.Add(".talk", "text/x-speech");
            data.Add(".tar", "application/x-tar");
            data.Add(".taz", "application/x-tar");
            data.Add(".tbp", "application/x-timbuktu");
            data.Add(".tbt", "application/x-timbuktu");
            data.Add(".tcl", "application/x-tcl");
            data.Add(".tex", "application/x-tex");
            data.Add(".texi", "application/x-texinfo");
            data.Add(".texinfo", "application/x-texinfo");
            data.Add(".tgz", "application/x-tar");
            data.Add(".thm", "application/vnd.eri.thm");
            data.Add(".tif", "image/tiff");
            data.Add(".tiff", "image/tiff");
            data.Add(".tki", "application/x-tkined");
            data.Add(".tkined", "application/x-tkined");
            data.Add(".toc", "application/toc");
            data.Add(".toy", "image/toy");
            data.Add(".tr", "application/x-troff");
            data.Add(".trk", "x-lml/x-gps");
            data.Add(".trm", "application/x-msterminal");
            data.Add(".tsi", "audio/tsplayer");
            data.Add(".tsp", "application/dsptype");
            data.Add(".tsv", "text/tab-separated-values");
            data.Add(".ttf", "application/octet-stream");
            data.Add(".ttz", "application/t-time");
            data.Add(".txt", "text/plain");
            data.Add(".ult", "audio/x-mod");
            data.Add(".ustar", "application/x-ustar");
            data.Add(".uu", "application/x-uuencode");
            data.Add(".uue", "application/x-uuencode");
            data.Add(".vcd", "application/x-cdlink");
            data.Add(".vcf", "text/x-vcard");
            data.Add(".vdo", "video/vdo");
            data.Add(".vib", "audio/vib");
            data.Add(".viv", "video/vivo");
            data.Add(".vivo", "video/vivo");
            data.Add(".vmd", "application/vocaltec-media-desc");
            data.Add(".vmf", "application/vocaltec-media-file");
            data.Add(".vmi", "application/x-dreamcast-vms-info");
            data.Add(".vms", "application/x-dreamcast-vms");
            data.Add(".vox", "audio/voxware");
            data.Add(".vqe", "audio/x-twinvq-plugin");
            data.Add(".vqf", "audio/x-twinvq");
            data.Add(".vql", "audio/x-twinvq");
            data.Add(".vre", "x-world/x-vream");
            data.Add(".vrml", "x-world/x-vrml");
            data.Add(".vrt", "x-world/x-vrt");
            data.Add(".vrw", "x-world/x-vream");
            data.Add(".vts", "workbook/formulaone");
            data.Add(".wav", "audio/x-wav");
            data.Add(".wax", "audio/x-ms-wax");
            data.Add(".wbmp", "image/vnd.wap.wbmp");
            data.Add(".web", "application/vnd.xara");
            data.Add(".wi", "image/wavelet");
            data.Add(".wis", "application/x-InstallShield");
            data.Add(".wm", "video/x-ms-wm");
            data.Add(".wma", "audio/x-ms-wma");
            data.Add(".wmd", "application/x-ms-wmd");
            data.Add(".wmf", "application/x-msmetafile");
            data.Add(".wml", "text/vnd.wap.wml");
            data.Add(".wmlc", "application/vnd.wap.wmlc");
            data.Add(".wmls", "text/vnd.wap.wmlscript");
            data.Add(".wmlsc", "application/vnd.wap.wmlscriptc");
            data.Add(".wmlscript", "text/vnd.wap.wmlscript");
            data.Add(".wmv", "video/x-ms-wmv");
            data.Add(".wmx", "video/x-ms-wmx");
            data.Add(".wmz", "application/x-ms-wmz");
            data.Add(".wpng", "image/x-up-wpng");
            data.Add(".wpt", "x-lml/x-gps");
            data.Add(".wri", "application/x-mswrite");
            data.Add(".wrl", "x-world/x-vrml");
            data.Add(".wrz", "x-world/x-vrml");
            data.Add(".ws", "text/vnd.wap.wmlscript");
            data.Add(".wsc", "application/vnd.wap.wmlscriptc");
            data.Add(".wv", "video/wavelet");
            data.Add(".wvx", "video/x-ms-wvx");
            data.Add(".wxl", "application/x-wxl");
            data.Add(".x-gzip", "application/x-gzip");
            data.Add(".xar", "application/vnd.xara");
            data.Add(".xbm", "image/x-xbitmap");
            data.Add(".xdm", "application/x-xdma");
            data.Add(".xdma", "application/x-xdma");
            data.Add(".xdw", "application/vnd.fujixerox.docuworks");
            data.Add(".xht", "application/xhtml+xml");
            data.Add(".xhtm", "application/xhtml+xml");
            data.Add(".xhtml", "application/xhtml+xml");
            data.Add(".xla", "application/vnd.ms-excel");
            data.Add(".xlc", "application/vnd.ms-excel");
            data.Add(".xll", "application/x-excel");
            data.Add(".xlm", "application/vnd.ms-excel");
            data.Add(".xls", "application/vnd.ms-excel");
            data.Add(".xlt", "application/vnd.ms-excel");
            data.Add(".xlw", "application/vnd.ms-excel");
            data.Add(".xm", "audio/x-mod");
            data.Add(".xml", "text/xml");
            data.Add(".xmz", "audio/x-mod");
            data.Add(".xpi", "application/x-xpinstall");
            data.Add(".xpm", "image/x-xpixmap");
            data.Add(".xsit", "text/xml");
            data.Add(".xsl", "text/xml");
            data.Add(".xul", "text/xul");
            data.Add(".xwd", "image/x-xwindowdump");
            data.Add(".xyz", "chemical/x-pdb");
            data.Add(".yz1", "application/x-yz1");
            data.Add(".z", "application/x-compress");
            data.Add(".zac", "application/x-zaurus-zac");
            data.Add(".zip", "application/zip");

            data.TryGetValue(extension.ToLower(), out type);
            return type;
        }

        #region
        private static string Boundary {
            get {
                string[] bArray, ctArray;
                string contentType = ContentType;
                ctArray = contentType.Split(';');
                if (ctArray[0].Trim().ToLower() == "multipart/form-data") {
                    bArray = ctArray[1].Split('=');
                    return "--" + bArray[1];
                }
                return null;
            }
        }

        private static string ContentType {
            get {
                return "multipart/form-data; boundary=-----------------------------7dd1221d9d0930";
            }
        }
        #endregion
    }
}
